/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.tapestry.view.components;

import com.javaforge.tapestry.spring.annotations.InjectSpring;
import com.kcp.ko.fmtnmlib.service.HibernatePersistenceService;
import org.apache.tapestry.BaseComponent;
import org.apache.tapestry.IAsset;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.Asset;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.annotations.Parameter;
import org.braintrain.domain.Content;
import org.braintrain.domain.Module;
import org.braintrain.domain.Navigation;
import org.braintrain.domain.TestData;
import org.braintrain.domain.types.NavigationType;
import org.braintrain.services.NavigationService;
import org.braintrain.tapestry.view.EditTest;
import org.braintrain.tapestry.view.View;

import java.util.List;

public abstract class NavigationChainContainer extends BaseComponent {

    @Asset("/images/add.gif")
    public abstract IAsset getAddImage();    
    
    @Parameter(required=true)
    public abstract Long getModuleId();
    public abstract void setModuleId(Long id);
    
    @Parameter(required=true)
    public abstract boolean isAddPages();
    public abstract void setAddPages(boolean v);
    
    @Parameter(required=true)
    public abstract boolean isReorderable();
    public abstract void setReorderable(boolean v);
    
    @Parameter(required=false)
    public abstract Long getCurrentNavigationId();
    public abstract void setCurrentNavigationId(Long id);
    
    @Parameter(required=false, defaultValue="null")
    public abstract String getChainCallback();
    public abstract void setChainCallback(String callback);
    
    @Parameter(required=false, defaultValue="null")
    public abstract String getPopupCallback();
    public abstract void setPopupCallback(String callback);

    @Parameter(required=false, defaultValue ="true")
    public abstract boolean isRenderMainNavigation();
    public abstract void setRenderMainNavigation(boolean v);
    
    @InjectSpring("navigationService")
    public abstract NavigationService getNavigationService();
    
    @InjectSpring("genericHibernateService")
    public abstract HibernatePersistenceService getGenericService();
    
    public abstract List<Navigation> getNavigationChain();
    public abstract void setNavigationChain(List<Navigation> l);
    
    //Page Injections
    @InjectPage("View")
    public abstract View getViewPage();
    
    @InjectPage("EditTest")
    public abstract EditTest getEditTestPage();
    
    /**
     * Return a list of list of navigations, the linked navigation chains
     * @return
     */
    public List<List<Navigation>> getLinkedPages()
    {
        if(getModuleId() != null)
        {
            Module module = (Module)getGenericService().getById(Module.class, getModuleId());
            return getNavigationService().getLinkedChains(module, true, false);
        }
        return null;
    }
    
    /**
     * Return a list of navigations, the main chain
     * @return
     */
    public List<Navigation> getInOrderMainPages()
    {
        if(getModuleId() != null)
        {
            Module module = (Module)getGenericService().getById(Module.class, getModuleId());
            return getNavigationService().getMainDeltaNavigationChain(module, true);
        }
        return null;
    }
    
    /**
     * Add a page to the Linked pages
     * @param cycle
     */
    public void addAdditionalPage(IRequestCycle cycle, Long moduleId)
    {
        Module module = (Module)getGenericService().getById(Module.class, moduleId);

        Navigation navigation = new Navigation();
        navigation.getNullSafeContent().setDeltaValue(new Content());
        navigation.setMainNavigation(false);
        navigation.setType(NavigationType.View);

        module.getLinkPages().add(navigation);

        navigation.setModule(module);

        getGenericService().saveOrUpdate(navigation);

        View view = getViewPage();
        view.setEditContent(true);
        view.activate(cycle, navigation.getId(), null);

    }

    /**
     * Add a test to the Linked pages
     * @param cycle
     */
    public void addAdditionalTest(IRequestCycle cycle, Long moduleId)
    {
        Module module = (Module)getGenericService().getById(Module.class, moduleId);

        Navigation navigation = new Navigation();
        navigation.getNullSafeContent().setDeltaValue(new TestData());
        navigation.setMainNavigation(false);
        navigation.setType(NavigationType.Test);

        module.getLinkPages().add(navigation);

        navigation.setModule(module);

        getGenericService().saveOrUpdate(navigation);

        getEditTestPage().activate(cycle, navigation.getId(), null);
    }
}
